package hong.jun.pet_home.service.org.impl;


import hong.jun.basic.base.service.impl.BaseServiceImpl;
import hong.jun.basic.util.AjaxResult;
import hong.jun.pet_home.domain.org.Department;
import hong.jun.pet_home.domain.org.Employee;
import hong.jun.pet_home.domain.org.Shop;
import hong.jun.pet_home.mapper.ShopMapper;
import hong.jun.pet_home.service.org.IEmployeeService;
import hong.jun.pet_home.service.org.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ShopMapper mapper;

    //商铺入驻注册  存店铺和管理员 状态为待激活
    public AjaxResult shopregister(Shop shop) {
        try {
            Department department = new Department();
            department.setId(1L);//店铺管理员所属部门设置为1
            //存店铺
            Employee admin = shop.getAdmin();
            admin.setDep(department);
            admin.setState(0);
            employeeService.save(admin);
            //存店铺管理员
            shop.setAdmin(admin);
            shop.setState(0);
            shop.setRegisterTime(new Date());
            mapper.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }

    }

    /**
     * 店铺和店铺管理员激活或禁用
     */
    @Override
    public AjaxResult audit(Shop shop) {
        Integer state = shop.getState();
        try {
            if (state == 0 || state == -1) {//状态码为0进行激活操作
                shop.setStateid(1);//设置冗余字段 将要更改的状态
                mapper.audit(shop);
            } else if (state == 1) {//状态码为01进行禁用操作
                shop.setStateid(-1);//设置冗余字段 将要更改的状态
                mapper.audit(shop);
            }
            employeeService.audit(shop.getAdmin());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }
}
